package _rabbitmq.aliyun_endpoint;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author huangya
 * @date 2024/9/11
 */
public class ConsumerDemo {

    private final static String VIRTUAL_HOST = "/scene_engine_pre";
    //    private final static String VIRTUAL_HOST = "/common";
    private final static String EXCHANGE_NAME = "scene_awareness_exchange_pre";

    private final static String ROUTING_KEY = "";

    public static void main(String[] argv) throws Exception {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        // RabbitMQ 服务器地址
        factory.setHost("1283899258956354.mq-amqp.cn-shanghai-a.aliyuncs.com");
        factory.setPort(5672);
        factory.setUsername("MjoxMjgzODk5MjU4OTU2MzU0OkxUQUk1dEt5SHE4bzM4cWU1dXVMYTk3dw==");
        factory.setPassword("QTEyNzdBMjg3RDE4RUFFMjJCQTU1OTIwMEZDOThCNzc5OUMxNkVGMToxNzI2Mjk2OTQwNTE5");
        factory.setVirtualHost(VIRTUAL_HOST);

        Connection connection = null;
        Channel channel = null;
        try {
            // 3: 从连接工厂中获取连接
            connection = factory.newConnection("生产者");
            // 4: 从连接中获取通道channel
            channel = connection.createChannel();

//            channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true);

            // 创建一个临时队列
            String queueName = channel.queueDeclare().getQueue();
//            String queueName = "scene_awareness_queue_pre";

            // 将队列绑定到交换机
            channel.queueBind(queueName, EXCHANGE_NAME, ROUTING_KEY);

            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

            DeliverCallback deliverCallback = new DeliverCallback() {
                @Override
                public void handle(String consumerTag, Delivery delivery) throws IOException {
                    String message = new String(delivery.getBody(), "UTF-8");
                    System.out.println(" [x] Received '" + message + "'");
                }
            };

            // 开始接收消息
            channel.basicConsume(queueName, true, deliverCallback, new CancelCallback() {
                @Override
                public void handle(String consumerTag) throws IOException {
                    System.out.println("1");
                }
            });
            TimeUnit.SECONDS.sleep(1000);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("发送消息出现异常...");
        } finally {
            // 7: 释放连接关闭通道
            if (channel != null && channel.isOpen()) {
                try {
                    channel.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (connection != null && connection.isOpen()) {
                try {
                    connection.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        System.out.println("222");
    }
}
